package com.zx.tradeclient.ctrl.index2;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.base.ctrl.BaseActivity;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.ActivityTaskManager;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.ctrl.index5.LoginActivity;
import com.zx.tradeclient.entity.BuyProductInfo;

public class BuyInfoActivity extends BaseActivity implements OnClickListener {
	/** Called when the activity is first created. */

	private TextView buy_info_name;
	private TextView buy_info_price;
	private TextView buy_info_number;
	private TextView buy_info_requirement;
	private TextView buy_info_package;
	private TextView buy_info_company;
	private TextView buy_info_contact;
	private TextView buy_info_phone;

	private Button buy_info_button;

	private String mProductID = "";
	private ProgressDialog progressDialog;
	private BuyProductInfo info;
	private TradeApp app;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.buy_product_info);

		if (!(ActivityTaskManager.getInstance().containsName("BuyInfoActivity"))) {
			ActivityTaskManager.getInstance().putActivity("BuyInfoActivity", this);
		}

		init();

	}

	private void init() {
		
		app = (TradeApp) getMyApplication();

		setHeader(0);
		header_left.setOnClickListener(this);
		header_right.setText("收藏");
		header_right.setVisibility(View.VISIBLE);
		header_right.setOnClickListener(this);

		mProductID = getIntent().getStringExtra("productid");

		buy_info_name = (TextView) findViewById(R.id.buy_info_name);
		buy_info_price = (TextView) findViewById(R.id.buy_info_price);
		buy_info_number = (TextView) findViewById(R.id.buy_info_number);
		buy_info_requirement = (TextView) findViewById(R.id.buy_info_requirement);
		buy_info_package = (TextView) findViewById(R.id.buy_info_package);
		buy_info_company = (TextView) findViewById(R.id.buy_info_company);
		buy_info_contact = (TextView) findViewById(R.id.buy_info_contact);
		buy_info_phone = (TextView) findViewById(R.id.buy_info_phone);

		buy_info_button = (Button) findViewById(R.id.buy_info_button);
		buy_info_button.setOnClickListener(this);

		progressDialog = ProgressDialog.show(BuyInfoActivity.this, null, "正在获取数据，请稍后.....", true, true);
		getSupplyProductInfo();
	}

	private void getSupplyProductInfo() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		map.put("act", "demandDetail");
		map.put("id", mProductID);

		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseJson(response);

			}

			@Override
			public void onFailure(Throwable e) {
				progressDialog.dismiss();
				Toast toast2 = Toast.makeText(BuyInfoActivity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			}
		});
	}

	private void parseJson(JSONObject response) {
		try {

			String succeed = response.getString("succeed");

			if (succeed.equals("000")) {

				info = new BuyProductInfo();

				info.setBuy_info_id(response.getString("id"));

				buy_info_name.setText(response.getString("title"));
				info.setBuy_info_name(response.getString("title"));

				buy_info_price.setText(response.getString("price"));
				info.setBuy_info_price(response.getString("price"));

				buy_info_number.setText(response.getString("num"));
				info.setBuy_info_number(response.getString("num"));

				buy_info_requirement.setText(response.getString("askContent"));
				info.setBuy_info_requirement(response.getString("askContent"));

				buy_info_package.setText(response.getString("packAsk"));
				info.setBuy_info_package(response.getString("packAsk"));

				info.setBuy_info_company_id(response.getString("userId"));

				buy_info_company.setText(response.getString("companyName"));
				info.setBuy_info_company(response.getString("companyName"));

				buy_info_contact.setText(response.getString("linkMan"));
				info.setBuy_info_contact(response.getString("linkMan"));

				buy_info_phone.setText(response.getString("linkMobile"));

			} else {
				Toast toast2 = Toast.makeText(BuyInfoActivity.this, "无数据", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();
			}

			progressDialog.dismiss();

		} catch (JSONException e) {
			progressDialog.dismiss();
			e.printStackTrace();
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == header_left) {
			goback();
		} else if (v == header_right) {
			if (app.user.getId().equals("")) {
				Intent intent = new Intent(BuyInfoActivity.this, LoginActivity.class);
				startActivity(intent);
			} else {
				progressDialog = ProgressDialog.show(BuyInfoActivity.this, null, "正在提交数据，请稍后.....", true, true);
				productFavorite();
			}

		} else if (v == buy_info_button) {
			if (info != null ) {
				if(app.user!=null && app.user.getId()!=null && app.user.getId()!=""){
					Intent intent = new Intent(BuyInfoActivity.this, BuyFromSupplyActivity.class);
					intent.putExtra("demandId", info.getBuy_info_id());
					intent.putExtra("productName", info.getBuy_info_name());
					intent.putExtra("companyName", info.getBuy_info_company());
					startActivity(intent);
				}else{
					Toast toast = Toast.makeText(BuyInfoActivity.this, "您尚未登录", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				}
			}

		}

	}
	
	private void productFavorite() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		map.put("act", "favourites");
		map.put("userId", app.user.getId());
		map.put("type", "4");
		map.put("infoId", mProductID);
		map.put("infoName", buy_info_name.getText().toString());

		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				try {
					String succeed = response.getString("succeed");

					if (succeed.equals("000")) {

						Toast toast2 = Toast.makeText(BuyInfoActivity.this, "收藏成功", Toast.LENGTH_SHORT);
						toast2.setGravity(Gravity.CENTER, 0, 0);
						toast2.show();

					} else {
						Toast toast2 = Toast.makeText(BuyInfoActivity.this, "无数据", Toast.LENGTH_SHORT);
						toast2.setGravity(Gravity.CENTER, 0, 0);
						toast2.show();
					}

					progressDialog.dismiss();

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(Throwable e) {
				progressDialog.dismiss();
				Toast toast2 = Toast.makeText(BuyInfoActivity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			}
		});
	}
}